import { register } from '@antv/x6-vue-shape';
import {
  NODE_WIDTH,
  NODE_HEIGHT,
  NODE_TYPE_START,
  NODE_TYPE_END,
  NODE_TYPE_INTERMEDIATE_END,
  ConditionTypeEnum,
  NodeTypeEnum,
} from '../constant/index.js';
import NodeView from '../../components/NodeView.vue';
import NodeToolBar from '../../components/NodeToolBar.vue';
import NodeBadge from '../../components/NodeBadge.vue';

// 导入所有节点配置
import { getIconByType, getStartIcon, getEndIcon } from './icons.js';

// 定义节点类型配置
const nodeConfigs = [
  // 开始和结束节点
  {
    type: NODE_TYPE_START,
    label: '开始',
    icon: getStartIcon(),
    width: 36,
    height: 36,
    primer: 'circle',
  },
  {
    type: NODE_TYPE_END,
    label: '结束',
    icon: getEndIcon(),
    width: 36,
    height: 36,
    primer: 'circle',
  },
  // 顺序类 - 使用 ConditionTypeEnum
  {
    type: ConditionTypeEnum.THEN,
    label: '串行(Then)',
    icon: getIconByType(ConditionTypeEnum.THEN),
  },
  {
    type: ConditionTypeEnum.WHEN,
    label: '并行(When)',
    icon: getIconByType(ConditionTypeEnum.WHEN),
  },
  // 分支类 - 使用 NodeTypeEnum
  {
    type: NodeTypeEnum.SWITCH,
    label: '选择(Switch)',
    icon: getIconByType(NodeTypeEnum.SWITCH),
  },
  {
    type: NodeTypeEnum.IF,
    label: '条件(If)',
    icon: getIconByType(NodeTypeEnum.IF),
  },
  // 循环类 - 使用 NodeTypeEnum
  {
    type: NodeTypeEnum.FOR,
    label: '次数循环(For)',
    icon: getIconByType(NodeTypeEnum.FOR),
  },
  {
    type: NodeTypeEnum.WHILE,
    label: '条件循环(While)',
    icon: getIconByType(NodeTypeEnum.WHILE),
  },
  {
    type: NodeTypeEnum.ITERATOR,
    label: '迭代循环(Iterator)',
    icon: getIconByType(NodeTypeEnum.ITERATOR),
  },
  // 其他类 - 使用 ConditionTypeEnum
  {
    type: ConditionTypeEnum.CATCH,
    label: '捕获异常(Catch)',
    icon: getIconByType(ConditionTypeEnum.CATCH),
  },
  {
    type: ConditionTypeEnum.AND,
    label: '与(And)',
    icon: getIconByType(ConditionTypeEnum.AND),
  },
  {
    type: ConditionTypeEnum.OR,
    label: '或(Or)',
    icon: getIconByType(ConditionTypeEnum.OR),
  },
  {
    type: ConditionTypeEnum.NOT,
    label: '非(Not)',
    icon: getIconByType(ConditionTypeEnum.NOT),
  },
  {
    type: ConditionTypeEnum.CHAIN,
    label: '子流程(Chain)',
    icon: getIconByType(ConditionTypeEnum.CHAIN),
  },
  {
    type: NodeTypeEnum.COMMON,
    label: '普通节点',
    icon: getIconByType(NodeTypeEnum.COMMON),
  },
  {
    type: NodeTypeEnum.VIRTUAL,
    label: '虚节点',
    icon: getIconByType(NodeTypeEnum.VIRTUAL),
  },
];

export const registerVueShapes = () => {
  // 注册所有节点
  nodeConfigs.forEach((config) => {
    const { type, label, icon, width = NODE_WIDTH, height = NODE_HEIGHT, primer } = config;
    
    register({
      shape: type,
      inherit: 'vue-shape',
      width,
      height,
      component: {
        render(h) {
          const node = this.getNode();
          return h(NodeView, {
            props: {
              icon: icon,
              node: node,
            },
          }, [
            h(NodeBadge, { props: { node } }),
            h(NodeToolBar, { props: { node } }),
          ]);
        },
        components: {
          NodeView,
          NodeBadge,
          NodeToolBar,
        },
        inject: ['getNode'],
      },
      attrs: {
        label: {
          refX: 0.5,
          refY: '100%',
          refY2: 20,
          text: label,
          fill: '#333',
          fontSize: 13,
          textAnchor: 'middle',
          textVerticalAnchor: 'middle',
          textWrap: {
            width: 80,
            height: 60,
            ellipsis: true,
            breakWord: true,
          },
        },
      },
      ...(primer ? { primer } : {}),
    });
  });
  
  // 单独注册 INTERMEDIATE_END（不需要工具栏）
  register({
    shape: NODE_TYPE_INTERMEDIATE_END,
    inherit: 'circle',
    width: 24,
    height: 24,
    attrs: {
      body: {
        fill: '#ced4da',
        stroke: '#adb5bd',
      },
    },
  });
};
